package com.test.test11.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.test.test11.DeviceListAdapter;
import com.test.test11.R;
import com.test.test11.SharedConstants;
import com.test.test11.entity.UserDeviceRelationship;
import com.test.test11.http.AfterRequest;
import com.test.test11.task.DeviceListGetTask;

import java.util.List;

/**
 * 显示设备列表
 */

public class DeviceActivity extends AppCompatActivity {

    private static final String TAG = "Device_Activity";

    private Button deviceScanBtn;     // 扫一扫按钮
    private ListView deviceListview;
    private List<UserDeviceRelationship> userDeviceRelationships;
    private DeviceListAdapter deviceListAdapter;
    public Handler listViewUpdateHandler;
    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_list);
        System.out.println("5463789697");

        if (SharedConstants.deviceList == null) {
            // 获取用户的设备列表信息
            DeviceListGetTask deviceListGetTask = new DeviceListGetTask(new AfterRequest() {
                @Override
                public void success() {

                }

                @Override
                public void failure(int code, String msg) {
                    if(code==-1){
                        Toast.makeText(DeviceActivity.this, "请检查网络连接", Toast.LENGTH_LONG).show();
                        return;
                    }
                }
            });

            deviceListGetTask.execute();
        }
       // View view = inflater.inflate(R.layout.device_list, deviceListview, false);
        deviceScanBtn = (Button)findViewById(R.id.bt_add_device);
        deviceListview = (ListView) findViewById(R.id.listview_device_list);
        userDeviceRelationships = SharedConstants.getDeviceList();
        //deviceListview.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1));
        deviceListAdapter = new DeviceListAdapter(DeviceActivity.this,userDeviceRelationships);
        //deviceListAdapter = new DeviceListAdapter();
        deviceListview.setAdapter(deviceListAdapter);


        // 列表显示更新的句柄
        listViewUpdateHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg!=null){
                    deviceListAdapter.updateList(SharedConstants.getDeviceList());
                }
            }
        };

    }

}
